#include "WatchDog.h"

const int wdtTimeout = 20000;//看门狗复位时间为20s
hw_timer_t *timer = NULL;

void IRAM_ATTR resetModule() 
{
  //ets_printf("ESP32 HEAP1 %d\r\n", esp_get_free_heap_size());
  //ets_printf("reboot\n");
  Serial.printf("ESP32 HEAP1 %d\r\n",esp_get_free_heap_size());
  Serial.println("reboot");
  esp_restart();
}

void WatchDog_Init(void)
{
  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  timerAlarmEnable(timer);                          //enable interrupt

  Serial.println("WatchDog Init success");
  //Serial.printf("WatchDog.cpp %d:ESP32 HEAP1:%d\r\n",__LINE__,esp_get_free_heap_size());
}


void Feed_Dog(void)
{
  timerWrite(timer,0);
}